﻿//  Copyright © Decebal Mihailescu 2013-2014
//  All rights reserved. This source code and binaries are distributed under 
//  Microsoft Public PatternLicense (Ms-PL)
//  The latest code is available at http://VSDesignPatterns.codeplex.com

//  Redistributions of source code must retain the above copyright notice, 
//  this list of conditions and the following disclaimer. 
//  Redistributions in binary form must reproduce the above copyright notice, 
//  this list of conditions and the following disclaimer in the documentation 
//  and/or other materials provided with the distribution. 

//  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
//  PURPOSE. IT CAN BE DISTRIBUTED FREE OF CHARGE AS LONG AS THIS HEADER 
//  REMAINS UNCHANGED.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace DesignPatternsWizard
{

    using Common;

    public partial class FilePage : TabPage
    {
        readonly TextBox _textBoxFileContent = new System.Windows.Forms.TextBox();
        readonly ITemplateFile _templateFile;
        public FilePage(ITemplateFile itf, int index)
        {
            try
            {

                InitializeComponent();
                this.SuspendLayout();
                this.Controls.Add(this._textBoxFileContent);
                this.Location = new System.Drawing.Point(4, 22);
                this.Name = itf.FileItemName;
                this.Padding = new System.Windows.Forms.Padding(3);
                this.Size = new System.Drawing.Size(450, 262);
                this.TabIndex = index;
                this.Text = itf.FileItemName;
                this.UseVisualStyleBackColor = true;
                this._textBoxFileContent.Dock = System.Windows.Forms.DockStyle.Fill;
                this._textBoxFileContent.ScrollBars = ScrollBars.Both;
                this._textBoxFileContent.Location = new System.Drawing.Point(3, 3);
                this._textBoxFileContent.Multiline = true;
                this._textBoxFileContent.Name = "_textBoxFileContent";
                this._textBoxFileContent.Size = new System.Drawing.Size(444, 256);
                this._textBoxFileContent.TabIndex = 0;
                if (this.DesignMode)
                    return;
                try
                {
                    _templateFile = itf;
                    ILanguageProvider lp = _templateFile.GetAsService<ILanguageProvider>();
                    ITabWizard itw = _templateFile.GetAsService<ITabWizard>();
                    ITemplatePattern itp = _templateFile.GetAsService<ITemplatePattern>();
                    if (string.IsNullOrEmpty(_templateFile.FileTemplateContent))
                        _textBoxFileContent.Text = string.Format("FileTemplate '{0}'is empty or missing", _templateFile.FileTemplate);
                    else
                    {
                        string txt = Utils.ExecuteArgSubstitution(_templateFile, itp.Arguments);
                        _textBoxFileContent.Text = txt;
                    }
                }
                catch (Exception ex)
                {
                    _textBoxFileContent.Text = ex.Message;
                }
            }
            finally
            {
                this.ResumeLayout(false);
                this.PerformLayout();
            }


        }

        protected override void OnLayout(LayoutEventArgs levent)
        {
            base.OnLayout(levent);
            if (this.DesignMode)
                return;
        }
    }
}
